<template>
  <div class="rule-builder">
    <div class="rule-operator">
      <el-radio-group v-model="rules.operator" size="small" fill="#5775e7">
        <el-radio-button label="且" value="and" />
        <el-radio-button label="或" value="or" />
      </el-radio-group>
    </div>
    <div class="rule-condition">
      <div class="condition-item" v-for="(item, index) of rules.conditions" :key="index">
        <span class="condition-num" :class="{ depth2: depth === 1 }">{{ index + 1 }}</span>
        <template v-if="item.operator">
          <RuleBuilder
            :taskData="taskData"
            :rules="item"
            :parentIndex="index"
            :depth="depth + 1"
            @on-remove-group="removeCondition(index)"
          />
        </template>
        <template v-else>
          <div class="condition-content">
            <div class="condition-set">
              <el-select
                v-model="item.measureId"
                clearable
                style="width: 180px"
                placeholder="请选择测点"
              >
                <el-option
                  v-for="measureItem in measureList"
                  :key="measureItem.id"
                  :label="measureItem.name"
                  :value="measureItem.id"
                />
              </el-select>
              <el-select
                v-model="item.signalId"
                clearable
                placeholder="请选逻辑传感器"
                style="width: 300px"
              >
                <el-option
                  v-for="sItem in getLogicSensorOptions(item.measureId)"
                  :key="sItem.signalId"
                  :label="
                    sItem.signalLabel +
                    '(' +
                    sItem.signalSourceList +
                    ')' +
                    '@' +
                    getMeasureName(item.measureId)
                  "
                  :value="sItem.signalId"
                />
              </el-select>
              <el-input v-model="item.label" style="width: 150px" placeholder="请输入告警标签" />
              <el-select v-model="item.op" placeholder="选择操作符" style="width: 70px">
                <el-option label="=" value="=" />
                <el-option label="!=" value="!=" />
                <el-option label=">" value=">" />
                <el-option label="<" value="<" />
                <el-option label=">=" value=">=" />
                <el-option label="<=" value="<=" />
              </el-select>

              <el-tag type="info" effect="plain" size="large"> 阈值 </el-tag>
            </div>
            <div class="condition-operate">
              <!-- <div class="add-condition" @click="addCondition(index)">
                  <el-icon color="#5775e7"><CirclePlus /></el-icon><span>新增</span>
                </div>
                <div class="add-condition" @click="addConditionGroup(index)">
                  <el-icon color="#5775e7"><CirclePlusFilled /></el-icon><span>新增组</span>
                </div> -->
              <div
                class="remove-condition"
                v-if="rules.conditions.length > 1"
                @click="removeCondition(index)"
              >
                <el-icon color="red"><Delete /></el-icon>
              </div>
            </div>
          </div>
        </template>
      </div>
    </div>
    <div class="condition-content condition-bottom">
      <div class="condition-set"></div>
      <div class="condition-operate">
        <div class="add-condition" @click="addCondition(rules.conditions.length)">
          <el-icon color="#5775e7"><CirclePlus /></el-icon><span>新增</span>
        </div>
        <div
          class="add-condition"
          v-if="depth === 0"
          @click="addConditionGroup(rules.conditions.length)"
        >
          <el-icon color="#5775e7"><CirclePlusFilled /></el-icon><span>新增组</span>
        </div>
        <div class="remove-condition" v-if="depth !== 0" @click="removeGroup()">
          <el-icon color="red"><Delete /></el-icon>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { CirclePlus, Delete, CirclePlusFilled } from '@element-plus/icons-vue'

const props = defineProps<{ rules: any; taskData: any; depth: number }>()

const { rules, taskData, depth } = toRefs(props)

const emit = defineEmits(['onRemoveGroup', 'change'])

const measureList = computed(() => {
  return taskData.value?.content.nodes
    .filter((item) => item.type === 'output')
    .map((item) => {
      console.log({
        id: item.id,
        ...item.data.measureData
      })
      return {
        id: item.id,
        ...item.data.measureData
      }
    })
})

const getLogicSensorOptions = (measureId) => {
  const measure = measureList.value.find((item) => item.id === measureId)
  // 获取signalList数组，若无则返回空数组，再过滤出signalType为'DIAG'的元素
  return (measure?.signalList || []).filter((signal) => signal.signalType === 'DIAG')
}

const getMeasureName = (measureId) => {
  return measureList.value.find((item) => item.id === measureId)?.name
}

/**
 * 新增条件
 * @param index
 */
const addCondition = (index) => {
  rules.value.conditions.splice(index + 1, 0, {
    signalId: '',
    label: '',
    op: '>'
  })
}

const addConditionGroup = (index) => {
  rules.value.conditions.splice(index + 1, 0, {
    operator: 'and',
    conditions: [
      {
        signalId: '',
        label: '',
        op: '>'
      }
    ]
  })
}

const removeCondition = (index) => {
  rules.value.conditions.splice(index, 1)
}

const removeGroup = () => {
  emit('onRemoveGroup')
}

watch(
  rules,
  (newVal) => {
    emit('change', newVal)
  },
  { deep: true }
)
</script>
<style lang="scss">
.rule-builder {
  height: auto;
  position: relative;
  min-height: 80px;
  border: 1px solid #f0f0f0;
  border-left: 1px solid #768ded;
  padding: 10px 10px 35px 10px;

  .rule-operator {
    position: absolute;
    top: 0;
    left: -13px;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    z-index: 1;

    .el-radio-group {
      .el-radio-button__inner {
        padding: 5px 0px;
        width: 26px;
        box-shadow: none !important;
      }

      .el-radio-button__inner:hover {
        color: #5775e7;
      }

      .el-radio-button {
        &.is-active {
          .el-radio-button__inner:hover {
            color: #fff;
          }
        }
      }

      .el-radio-button:first-child .el-radio-button__inner {
        border-radius: var(--el-border-radius-base) var(--el-border-radius-base) 0 0;
        border-bottom: 0;
      }

      .el-radio-button:last-child .el-radio-button__inner {
        border-radius: 0 0 var(--el-border-radius-base) var(--el-border-radius-base);
        border-left: var(--el-border);
      }
    }
  }

  .rule-condition {
    margin-left: 40px;
  }

  .condition-item {
    margin-bottom: 10px;
    position: relative;

    .condition-num {
      width: 15px;
      height: 15px;
      border-radius: 15px;
      line-height: 15px;
      text-align: center;
      font-size: 12px;
      position: absolute;
      left: -20px;
      top: 2px;
      background-color: #c4e538;
      color: #fff;

      &.depth2 {
        background-color: #ffc312;
      }
    }
  }

  .condition-content {
    display: flex;
    align-items: center;

    &.condition-bottom {
      position: absolute;
      bottom: 10px;
      right: 10px;
    }
  }

  .condition-set {
    display: flex;
    flex: 1;

    & > div {
      margin-right: 5px;
    }
  }

  .condition-operate {
    display: flex;
    align-items: center;

    & > div {
      display: flex;
      align-items: center;
      margin-left: 10px;
      cursor: pointer;

      & > span {
        margin-left: 4px;
      }
    }
  }
}
</style>
